﻿using System;

public partial class RequestServerController
{
    public void ReqViewApply(Apply data, Action<Apply> callback)
    {
        data.viewTime = UnityFunctions.GetTimeStampSeconds();
        ReqUpdateApply(data, callback);
    }
    public void ReqUpdateApply(Apply data, Action<Apply> callback)
    {
        Request_UpdateApply request = new Request_UpdateApply();
        request.applyReqData.apply = data;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Update Apply onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                MGame.instance.MUser.UpdateApply(data);

                if(data.result == Apply.ApplyResult_Suc)
                {
                    long oppoUserId = data.sourceUserId == MGame.instance.MUser.id?
                        data.targetUserId : data.sourceUserId;

                    MGame.instance.MUser.AddFriend(oppoUserId);
                }

                callback?.Invoke(data);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Apply onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }
    public void ReqSendDialogue(long fromId, long toId, string content, Action<Chat> callback)
    {
        Request_Dialogue request = new Request_Dialogue();
        long time = UnityFunctions.GetTimeStampSeconds();

        request.dialogueReqData.fromUserId = fromId;
        request.dialogueReqData.toUserId = toId;
        request.dialogueReqData.dialogue = new Dialogue( content, time);

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Dialogue onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                DialogueResponseData dialogueResponseData = response.responseData as DialogueResponseData;

                var chat = MGame.instance.MUser.AddDialogue(fromId, toId, content, time);

                callback?.Invoke(chat);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Dialogue onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqUpdateRequirement(Requirement data, Action<Requirement> callback)
    {
        Request_UpdateRequirement request = new Request_UpdateRequirement();
        request.reReqData.requirement = data;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_UpdateRequirement Apply onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                MGame.instance.MUser.UpdateRequire(data);

                callback?.Invoke(data);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_UpdateRequirement onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }


    public void ReqVisit(long fromUserId,long toUserId, Action<Visit> callback)
    {
        Request_Visit request = new Request_Visit();
        request.visitReqData.fromUserId = fromUserId;
        request.visitReqData.toUserId = toUserId;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Visit  onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                VisitResponseData data = response.responseData as VisitResponseData;

                if(fromUserId == MGame.instance.MUser.id)
                    MGame.instance.MUser.AddMyVisit(data.visit);
                else
                    MGame.instance.MUser.AddVisitor(data.visit);

                callback?.Invoke(data.visit);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(null);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Visit onTimeOut ");
            callback?.Invoke(null);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

    public void ReqLike(long fromUserId, long toUserId, Action<long> callback)
    {
        Request_Like request = new Request_Like();
        request.likeReqData.fromUserId = fromUserId;
        request.likeReqData.toUserId = toUserId;

        request.onResponse = delegate (ResponseBase response)
        {
            GameDebug.Log("Request_Like  onResponse:" + response.responseData.code);

            if (response.responseData.code == 0)
            {
                MGame.instance.MUser.AddLike(toUserId);
                
                callback?.Invoke(toUserId);
            }
            else
            {
                MGame.instance.ShowNoticePanel("网络出错，请重试");
                callback?.Invoke(-1);
            }
        };
        request.onTimeOut = delegate (RequestBase req)
        {
            GameDebug.Log("Request_Like onTimeOut ");
            callback?.Invoke(-1);
        };

        request.clientSocket = MGame.instance.clientSocket;
        request.timeOutSeconds = 12;
        request.Send();
    }

}

